package com.xyxe.domain.strategy.service.rule.tree.impl;

import com.xyxe.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import com.xyxe.domain.strategy.model.valobj.StrategyAwardStockKeyVO;
import com.xyxe.domain.strategy.repository.IStrategyRepository;
import com.xyxe.domain.strategy.service.armory.IStrategyDispatch;
import com.xyxe.domain.strategy.service.rule.tree.ILogicTreeNode;
import com.xyxe.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Slf4j
@Component("rule_stock")
public class RuleStockLogicTreeNode implements ILogicTreeNode {

    @Resource
    private IStrategyDispatch strategyDispatch;
    @Resource
    private IStrategyRepository repository;
    @Override
    public DefaultTreeFactory.TreeActionEntity logic(String userId, Long strategyId, Integer awardId, String ruleValue) {
        log.info("规则过滤-库存扣减 userId: {}, strategyId: {}, awardId:{}", userId, strategyId,awardId);

        // 扣减库存
        Boolean status = strategyDispatch.subtractionAwardStock(strategyId, awardId);
        // status   true 扣减成功
        if (status) {
            // 写入延迟队列，延迟消费更新数据库
            repository.awardStockConsumeSendQueue(StrategyAwardStockKeyVO.builder()
                    .strategyId(strategyId).awardId(awardId).build());

            return DefaultTreeFactory.TreeActionEntity.builder()
                    .ruleLogicCheckTypeVO(RuleLogicCheckTypeVO.TAKE_OVER)
                    .strategyAwardVO(DefaultTreeFactory.StrategyAwardVO.builder()
                            .awardId(awardId).awardRuleValue("").build())
                    .build();
        }

        return DefaultTreeFactory.TreeActionEntity.builder()
                .ruleLogicCheckTypeVO(RuleLogicCheckTypeVO.ALLOW)
                .build();
    }
}
